package com.pms.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.pms.pojo.Department;
import com.pms.pojo.DeptTransfer;
import com.pms.pojo.DeptTurn;
import com.pms.pojo.Employees;
import com.pms.pojo.FindRequire;
import com.pms.pojo.Require;
import com.pms.service.DeptService;

@RequestMapping("/dept")
@Controller
public class DeptHandler {
	
	@Resource(name="deptService")
	private DeptService deptService;
	
	
	public void setDeptService(DeptService deptService) {
		this.deptService = deptService;
	}

	@RequestMapping("/out")
	 public String out(Model  model) {
		List<Employees> employees = this.deptService.getEmployees();
		List<Department> departments = this.deptService.getDepartment();
		model.addAttribute("employees", employees);
		model.addAttribute("departments", departments);
		return "dept_index";
	}
	 
	@RequestMapping("/getRequire")
	public String getRequire(Require require,Model model) {
		
		List<Department> departments = this.deptService.getDepartment();
		List<Employees> employees = this.deptService.getRequireEmployees(require);
		model.addAttribute("employees", employees);
		model.addAttribute("departments", departments);
		return "dept_index";
	}
	@RequestMapping("/getUpdate")
	public String updateDeptartment(Integer emp_id,Model model) {
		Employees employees = this.deptService.getEmployeeById(emp_id);
		List<Department> departments = this.deptService.getDepartment();
		model.addAttribute("employees", employees);
		model.addAttribute("departments", departments);
		
		return "dept_transfer";
	}
	
	@RequestMapping("/updateAndAdd")
	public String updateAndAdd(DeptTransfer deptTransfer,Model model) {
		this.deptService.updateEmployee(deptTransfer);
		Department department =  this.deptService.getDept(deptTransfer.getDt_dept_a());
		deptTransfer.setDt_dept_name(department.getDept_name());
		System.out.println(department.toString());
		System.out.println(deptTransfer.toString());
		this.deptService.addDeptTransfer(deptTransfer);
		model.addAttribute("success", "success");
		//return "dept_index";
		return "dept_transfer";
	}
	
	@RequestMapping("/getDeptTranfer")
	public String getDeptTranfer(Model model) {
		List<DeptTurn> deptTurn = this.deptService.getDeptTranfer();
		model.addAttribute("deptTurn", deptTurn);
		return "dept_find";
	}
	@RequestMapping("/getByRequire")
	public String getByRequire(FindRequire findrequire,Model model) {
		List<DeptTurn> deptTurn = this.deptService.getByRequire(findrequire);
		model.addAttribute("deptTurn", deptTurn);
		System.out.println(findrequire.toString());
		return "dept_find";
	}
	
	
}
